package com.fast.cron;

import com.fast.cron.servlet.IndexServlet;
import com.fast.cron.servlet.LoginServlet;
import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.nio.SelectChannelConnector;
import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.ServletHolder;


/**
 * the web server of fast cron
 *
 * @author :leo.jie
 * @version :v1.0
 * @date :2018/9/30 15:18
 */
public class FastCronWebServer {
    public void runServer(String[] args) {
        Server server = execServer(args);
        try {
            server.start();
            server.join();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Server execServer(String[] args) {
        Server server = new Server();
        Connector connector = new SelectChannelConnector();
        connector.setHost("0.0.0.0");
        connector.setPort(8888);
        connector.setMaxIdleTime(3600 * 60 * 60);
        server.addConnector(connector);

        Context context = new Context();
        context.setContextPath("/");
        context.addServlet(new ServletHolder(new IndexServlet()), "/");
        context.addServlet(new ServletHolder(new IndexServlet()), "/index");
        context.addServlet(new ServletHolder(new LoginServlet()), "/login");
        server.setHandler(context);
        return server;
    }
}
